import {
  getUserProfile,
} from "../../utils/asyncWx.js";
import { callFunction, search } from "../../utils/cloud";
Page({
    data: {
        userInfo:[]
    },
    async getUserInfo (e) {
        try {
            let { userInfo } = await getUserProfile({ desc: "获取用户信息" });
            if (userInfo.nickName == null) {
                return
            }
            const { result } = await callFunction({ name: "login" });
        
            wx.setStorageSync("userInfo", ({
                nickName: userInfo.nickName,
                avatarUrl: userInfo.avatarUrl,
                openid: result.openid,
            }));
        
            this.setData({
                userInfo: {
                    nickName: userInfo.nickName,
                    avatarUrl: userInfo.avatarUrl,
                    openid: result.openid,
                }
            });
            const { data } = await search({
                table: "user",
                openid: result.openid,
            });
            //   如果不存在,则添加用户数据
            if (data.length === 0) {
                //   console.log("添加成功");
                insert({
                    table: "user",
                    data: {
                        nickName: userInfo.nickName,
                        avatarUrl: userInfo.avatarUrl,
                    },
                });
            }
            wx.navigateBack({
                delta: 1,
            });
        } catch (error) {
            console.error(error);
        }
    },
});